package com.lxg.dao;

import com.lxg.pojo.Admin;
import com.lxg.pojo.User;
import org.apache.ibatis.annotations.*;

import java.awt.*;
import java.util.ArrayList;

/**
 * 有关读者账号的连接数据库操作，登录验证，注册，修改账号，修改密码
 */
@Mapper
public interface UserDao {

    /**
     * 根据传入的账号，密码，来查找对应的用户信息，返回一个User类型
     * @param username
     * @param password
     */
    @Select("select * from user where username=#{username} and password=#{password}")
    public User getUser(String username,String password);

    /**
     * 获取全部用户的全部信息
     *
     * @return
     */
    @Select("select * from user where is_deleted = 0 order by times desc")
    public ArrayList<User> getAllUser();

    /**
     * 根据传入的 uid，查找对应用户
     *
     * @param uid
     * @return
     */
    @Select("select * from user where uid=#{uid}")
    public User getUserByUid(Integer uid);

    /**
     * 根据uid模糊查询用户
     * @param uid
     * @return
     */


    /**
     * 根据name模糊查询用户
     * @param name
     * @return
     */
    @Select("select * from user where  is_deleted = 0 and name like CONCAT('%',#{name},'%')")
    public ArrayList<User> getAllLikeUserByName(String name);

    /**
     * 根据username模糊查询用户
     * @param username
     * @return
     */
    @Select("select * from user where  is_deleted = 0 and username  like CONCAT('%',#{username},'%')")
    public ArrayList<User> getAllLikeUserByUsername(String username);

    /**
     * 根据sno查询用户
     * @param sno
     * @return
     */
    @Select("select * from user where sno=#{sno}")
    public User getUserBySno(String sno);

    /**
     * 检查该用户名是否已经被占用
     * 注册时使用
     * @param username
     * @return
     */
    @Select("select * from user where username=#{username}")
    public User checkUsername(String username);

    /**
     * 检查该邮箱是否已经被占用
     * 注册时使用
     * @param email
     * @return
     */
    @Select("select * from user where email=#{email}")
    public User checkEmail(String email);

    /**
     * 检查该手机号是否已经被占用
     * 注册时使用
     * @param phone
     * @return
     */
    @Select("select * from user where phone=#{phone}")
    public User checkPhone(String phone);


    @Select("select * from user where sno=#{sno}")
    public User checkSno(String sno);
    /**
     * 添加一个用户
     * @param username
     * @param password
     * @param name
     * @param sno
     * @param grade
     * @param email
     * @param phone
     */
    @Insert("insert into user (username,password,name,sno,grade,email,phone) values " +
            "(#{username},#{password},#{name},#{sno},#{grade},#{email},#{phone})")
    public void addUser(String username , String password, String name,String sno,
                        String grade, String email,String phone);

    /**
     * 根据uid删除一个用户
     * @param uid
     */
    @Update("update user set is_deleted=1 where uid=#{uid}")
    public void deleteUserByUid(Integer uid);

    /**
     * 用户修改自身信息
     * @param username
     * @param password
     * @param name
     * @param sno
     * @param grade
     * @param email
     * @param phone
     */
    @Update("update user set username=#{username},password=#{password},name=#{name}," +
            "sno=#{sno},grade=#{grade},email=#{email},phone=#{phone} " +
            "where uid=#{uid}")
    public void updateUser(String username,String password, String name,
                           String sno, String grade, String email,String phone,Integer uid);

    /**
     * 修改读者已借阅数量
     * @param uid
     * @param times
     */
    @Update("update user set times=#{times} where uid=#{uid}")
    public void updateUserTimes(Integer uid,Integer times);

    @Select("select count(*) from user")
    public Integer getNum();


    @Update("update user set password=#{password},name=#{name}," +
            "sno=#{sno},grade=#{grade},email=#{email},phone=#{phone},username=#{username} " +
            "where uid=#{uid}")
    public void adminUpdateUser(String username, String password, String name, String sno, String grade, String email, String phone,Integer uid);
}
